package com.situ.supermarket.service.impl;



import com.situ.supermarket.dao.UserDao;
import com.situ.supermarket.entity.User;
import com.situ.supermarket.service.UserService;
import com.situ.supermarket.util.StringUtil;
import com.situ.supermarket.vo.ResultVO;
import com.situ.supermarket.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;


public class UserServiceImpl implements UserService {
    UserDao dao=new UserDao();
    @Override
    public TableVO select(Integer page, Integer limit, String username, String name) {
        List<User> list=dao.select(page,limit,username,name);
        TableVO vo=new TableVO();
        vo.setCount(dao.count(username,name));
        vo.setData(list);
        return vo;
    }
    @Override
    public TableVO save(User u) {
        if(u.getId()==null){
            //判断用户名是否重复
            u.setCreateTime(LocalDateTime.now());
            u.setPassword(StringUtil.md5(u.getPassword()));
            dao.insert(u);
        }else {
            dao.update(u);
        }
        return null;
    }

    @Override
    public ResultVO delete(Integer id) {
        List<User> list = dao.selectById(id);
        User u = list.get(0);
        ResultVO vo = new ResultVO();
        if (u.getUsername().equals("user1")){
            vo.setCode(2);
            vo.setMsg("此账号为管理员账号，无法删除");
        }else {
            dao.delete(id);
        }
        return vo;
    }

    @Override
    public User info(Integer id) {
        List<User> list= dao.selectById(id);
        if(list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public boolean check(String username, String password) {
        List<User> list = dao.selectByUsername(username);
        if (list.isEmpty()){//说明用户名不对
            return false;
        }
        User user = list.get(0);
        return user.getPassword().equals(StringUtil.md5(password));
    }

    @Override
    public TableVO UpdatePassword(String oldPassword, String newPassword, String newPasswordAgain, HttpServletRequest request, HttpServletResponse response) {
        TableVO vo = new TableVO();
        User user =selectByUsername((String) request.getSession().getAttribute("currentUser"));
        if(!user.getPassword().equals(StringUtil.md5(oldPassword))){
            vo.setCode(2);
            vo.setMsg("原始密码输入错误");
            return vo;
        }
        if(!newPassword.equals(newPasswordAgain)){
            vo.setCode(2);
            vo.setMsg("两次输入输入不一致");
            return vo;
        }
        if (newPassword.length()>16 || newPassword.length()<6) {
            vo.setCode(2);
            vo.setMsg("修改失败，密码应在6-16位");
            return vo;
        }
        vo.setCode(0);
        vo.setMsg("密码修改成功");
        request.getSession().getAttribute("currentUser");
        dao.UpdatePassword(StringUtil.md5(newPassword),user.getId());
        return vo;
    }

    @Override
    public User selectByUsername(String username) {
        List<User> users =  dao.selectByUsername(username);
        for (User user : users) {
            return user;
        }
        return null;
    }

    @Override
    public User selectUsername(String username) {
        List<User> list = dao.selectByUsername(username);
        return list.isEmpty()?null:list.get(0);
    }
}
